<template>
  <div class="page-container">
    <button class="back-button" @click="goToHome">
      <span class="icon-arrow-left"></span>
      返回首页
    </button>
    
    <div class="page-header">
      <h1>工作分组</h1>
      <p>创建和管理不同的工作小组，灵活分配团队成员</p>
    </div>

    <div class="action-bar">
      <button class="primary-btn" @click="showAddGroupModal = true">
        <i class="icon-plus"></i> 新建工作分组
      </button>
      <div class="search-filter">
        <div class="search-box">
          <input type="text" v-model="searchKeyword" placeholder="搜索分组...">
          <button class="search-btn"><i class="icon-search"></i></button>
        </div>
        <select v-model="teamFilter" class="filter-select" @change="filterGroups">
          <option value="">所有团队</option>
          <option v-for="team in teams" :key="team.id" :value="team.id">{{ team.name }}</option>
        </select>
      </div>
    </div>

    <div class="groups-grid">
      <div class="group-card" v-for="group in filteredGroups" :key="group.id">
        <div class="group-header">
          <div>
            <h3>{{ group.name }}</h3>
            <span class="team-tag">{{ getTeamName(group.teamId) }}</span>
          </div>
          <div class="group-actions">
            <button class="icon-btn" @click="editGroup(group)"><i class="icon-edit"></i></button>
            <button class="icon-btn danger" @click="deleteGroup(group.id)"><i class="icon-trash"></i></button>
          </div>
        </div>
        
        <div class="group-info">
          <p><strong>组长:</strong> {{ group.leader }}</p>
          <p><strong>成员数:</strong> {{ group.members.length }}</p>
          <p><strong>创建时间:</strong> {{ group.createTime }}</p>
          <p><strong>描述:</strong> {{ group.description || '无描述' }}</p>
        </div>
        
        <div class="members-list">
          <div class="member-avatar" v-for="member in group.members" :key="member.id" :title="member.name">
            {{ getInitials(member.name) }}
          </div>
          <div class="member-count" v-if="group.members.length > 5">
            +{{ group.members.length - 5 }}
          </div>
        </div>
        
        <div class="group-footer">
          <button class="secondary-btn" @click="manageGroupMembers(group.id)">管理成员</button>
        </div>
      </div>
      
      <div class="empty-state" v-if="filteredGroups.length === 0">
        <i class="icon-group"></i>
        <p>暂无工作分组数据</p>
        <button class="primary-btn" @click="showAddGroupModal = true">创建第一个工作分组</button>
      </div>
    </div>

    <!-- 添加/编辑工作分组弹窗 -->
    <div class="modal-backdrop" v-if="showAddGroupModal">
      <div class="modal">
        <div class="modal-header">
          <h3>{{ isEditing ? '编辑工作分组' : '新建工作分组' }}</h3>
          <button class="close-btn" @click="showAddGroupModal = false">&times;</button>
        </div>
        <div class="modal-body">
          <form @submit.prevent="saveGroup">
            <div class="form-group">
              <label>分组名称 *</label>
              <input type="text" v-model="currentGroup.name" required>
            </div>
            <div class="form-group">
              <label>所属团队 *</label>
              <select v-model="currentGroup.teamId" required>
                <option value="">请选择团队</option>
                <option v-for="team in teams" :key="team.id" :value="team.id">{{ team.name }}</option>
              </select>
            </div>
            <div class="form-group">
              <label>分组组长 *</label>
              <input type="text" v-model="currentGroup.leader" required>
            </div>
            <div class="form-group">
              <label>分组描述</label>
              <textarea v-model="currentGroup.description" rows="3"></textarea>
            </div>
            <div class="modal-footer">
              <button type="button" class="secondary-btn" @click="showAddGroupModal = false">取消</button>
              <button type="submit" class="primary-btn">保存</button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'WorkGroup',
  data() {
    return {
      searchKeyword: '',
      teamFilter: '',
      showAddGroupModal: false,
      isEditing: false,
      currentGroup: {
        id: null,
        name: '',
        teamId: '',
        leader: '',
        description: '',
        members: []
      },
      teams: [
        { id: 1, name: '前端开发团队' },
        { id: 2, name: '后端开发团队' },
        { id: 3, name: '测试团队' }
      ],
      groups: [
        {
          id: 1,
          name: 'PC端开发组',
          teamId: 1,
          leader: '赵六',
          createTime: '2023-03-10',
          description: '负责公司PC端产品开发',
          members: [
            { id: 1, name: '赵六' },
            { id: 2, name: '钱七' },
            { id: 3, name: '孙八' },
            { id: 4, name: '周九' },
            { id: 5, name: '吴十' },
            { id: 6, name: '郑一' }
          ]
        },
        {
          id: 2,
          name: '移动端开发组',
          teamId: 1,
          leader: '王二',
          createTime: '2023-03-15',
          description: '负责公司移动端产品开发',
          members: [
            { id: 7, name: '王二' },
            { id: 8, name: '冯三' }
          ]
        },
        {
          id: 3,
          name: 'API开发组',
          teamId: 2,
          leader: '陈四',
          createTime: '2023-03-20',
          description: '负责公司API接口开发',
          members: [
            { id: 9, name: '陈四' },
            { id: 10, name: '褚五' }
          ]
        }
      ]
    };
  },
  computed: {
    filteredGroups() {
      return this.groups.filter(group => {
        const matchesSearch = !this.searchKeyword || 
          group.name.toLowerCase().includes(this.searchKeyword.toLowerCase()) ||
          group.leader.toLowerCase().includes(this.searchKeyword.toLowerCase()) ||
          (group.description && group.description.toLowerCase().includes(this.searchKeyword.toLowerCase()));
        
        const matchesTeam = !this.teamFilter || group.teamId === parseInt(this.teamFilter);
        
        return matchesSearch && matchesTeam;
      });
    }
  },
  methods: {
    goToHome() {
      this.$router.push('/home');
    },
    getTeamName(teamId) {
      const team = this.teams.find(t => t.id === teamId);
      return team ? team.name : '未知团队';
    },
    getInitials(name) {
      return name.charAt(0);
    },
    filterGroups() {
      // 由computed属性自动处理过滤
    },
    editGroup(group) {
      this.isEditing = true;
      this.currentGroup = { ...group };
      this.showAddGroupModal = true;
    },
    deleteGroup(id) {
      if (confirm('确定要删除这个工作分组吗？')) {
        this.groups = this.groups.filter(group => group.id !== id);
      }
    },
    manageGroupMembers(groupId) {
      // 跳转到分组成员管理页面
      this.$router.push(`/group-members/${groupId}`);
    },
    saveGroup() {
      if (this.isEditing) {
        const index = this.groups.findIndex(g => g.id === this.currentGroup.id);
        this.groups.splice(index, 1, this.currentGroup);
      } else {
        this.currentGroup.id = Date.now();
        this.currentGroup.createTime = new Date().toLocaleDateString();
        this.currentGroup.members = [];
        this.groups.push(this.currentGroup);
      }
      this.resetModal();
    },
    resetModal() {
      this.showAddGroupModal = false;
      this.isEditing = false;
      this.currentGroup = {
        id: null,
        name: '',
        teamId: '',
        leader: '',
        description: '',
        members: []
      };
    }
  }
};
</script>

<style scoped>
.page-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.page-header {
  margin: 20px 0 30px;
}

.page-header h1 {
  color: #2c3e50;
  margin-bottom: 8px;
}

.page-header p {
  color: #7f8c8d;
  font-size: 14px;
}

.action-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 15px;
}

.search-filter {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

.search-box {
  display: flex;
  align-items: center;
}

.search-box input {
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px 0 0 4px;
  outline: none;
  width: 250px;
}

.search-box input:focus {
  border-color: #3498db;
}

.search-btn {
  background: #3498db;
  color: white;
  border: none;
  border-radius: 0 4px 4px 0;
  padding: 8px 12px;
  cursor: pointer;
}

.filter-select {
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  outline: none;
  background: white;
}

.groups-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 20px;
  margin-top: 20px;
}

.group-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  transition: transform 0.3s, box-shadow 0.3s;
}

.group-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.group-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 15px 20px;
  background: #f8f9fa;
  border-bottom: 1px solid #eee;
}

.group-header h3 {
  margin: 0 0 5px 0;
  color: #2c3e50;
}

.team-tag {
  display: inline-block;
  background: #ebf5fb;
  color: #3498db;
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 12px;
}

.group-info {
  padding: 15px 20px;
}

.group-info p {
  margin: 8px 0;
  font-size: 14px;
  color: #555;
}

.members-list {
  padding: 0 20px 15px;
  display: flex;
  gap: 8px;
  align-items: center;
}

.member-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: #3498db;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 14px;
}

.member-count {
  font-size: 12px;
  color: #7f8c8d;
  background: #f1f2f6;
  padding: 2px 8px;
  border-radius: 12px;
}

.group-footer {
  padding: 12px 20px;
  background: #f8f9fa;
  border-top: 1px solid #eee;
  text-align: right;
}

.icon-btn {
  background: none;
  border: none;
  color: #7f8c8d;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: all 0.2s;
}

.icon-btn:hover {
  color: #3498db;
  background: #f1f1f1;
}

.icon-btn.danger:hover {
  color: #e74c3c;
}

.empty-state {
  text-align: center;
  padding: 50px 20px;
  color: #7f8c8d;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  grid-column: 1 / -1;
}

.empty-state i {
  font-size: 48px;
  margin-bottom: 15px;
  color: #bdc3c7;
}

/* 按钮样式 */
.primary-btn {
  background: #3498db;
  color: white;
  border: none;
  border-radius: 4px;
  padding: 8px 16px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: all 0.2s;
}

.primary-btn:hover {
  background: #2980b9;
  transform: translateY(-1px);
}

.secondary-btn {
  background: #ecf0f1;
  color: #2c3e50;
  border: none;
  border-radius: 4px;
  padding: 8px 16px;
  cursor: pointer;
  transition: all 0.2s;
}

.secondary-btn:hover {
  background: #dcdde1;
}

/* 弹窗样式 */
.modal-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal {
  background: white;
  border-radius: 8px;
  width: 90%;
  max-width: 500px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.modal-header {
  padding: 15px 20px;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-body {
  padding: 20px;
}

.form-group {
  margin-bottom: 15px;
}

.form-group label {
  display: block;
  margin-bottom: 6px;
  font-weight: 500;
  color: #2c3e50;
}

.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
}

.form-group textarea {
  resize: vertical;
}

.modal-footer {
  padding: 15px 20px;
  border-top: 1px solid #eee;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.close-btn {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #7f8c8d;
}

.back-button {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 15px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s;
  margin-right: 20px;
  font-weight: 500;
}

.back-button:hover {
  background: #2980b9;
  transform: translateY(-2px);
}

/* 图标样式模拟 */
.icon-plus::before { content: "+"; }
.icon-search::before { content: "🔍"; }
.icon-edit::before { content: "✏️"; }
.icon-trash::before { content: "🗑️"; }
.icon-arrow-left::before { content: "←"; }
.icon-group::before { content: "👪"; }
</style>